﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace hozorghiab
{
    public partial class mission : Form
    {
        public sql sqlquery;
        DataTable table;
        SqlDataAdapter da;
        DataGridViewButtonColumn deleteButton, editButton;
        BindingSource bs = new BindingSource();
        AutoCompleteStringCollection namesCollection = new AutoCompleteStringCollection();
        AutoCompleteStringCollection familyCollection = new AutoCompleteStringCollection();
        bool load = false;
        public mission(sql s)
        {
            InitializeComponent();
            sqlquery = s;

        }
        void setheader()
        {
            dataGridView1.Columns[0].HeaderText = "id";
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;

        }


        private void mission_Load(object sender, EventArgs e)
        {
            SqlDataReader dReader;
            sqlquery.open();
            dReader = sqlquery.ExecuteQuery("Select distinct Name from profile order by Name asc");

            if (dReader.HasRows == true)
            {
                while (dReader.Read())
                    namesCollection.Add(dReader["Name"].ToString().Trim());
            }
            else
            {
                MessageBox.Show("کاربری در سیستم ثبت نشده");
            }
            dReader.Close();

            dReader = sqlquery.ExecuteQuery("Select distinct family from profile order by family asc");
            if (dReader.HasRows == true)
            {
                while (dReader.Read())
                    familyCollection.Add(dReader["family"].ToString().Trim());
            }
            else
            {
                MessageBox.Show("کاربری در سیستم ثبت نشده");
            }
            dReader.Close();
            textBox10.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox10.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox10.AutoCompleteCustomSource = namesCollection;
            textBox2.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox2.AutoCompleteCustomSource = namesCollection;

            textBox13.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox13.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox13.AutoCompleteCustomSource = familyCollection;

            textBox11.AutoCompleteMode = AutoCompleteMode.Suggest;
            textBox11.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox11.AutoCompleteCustomSource = familyCollection;
            sqlquery.close();

        }

        private void textBox10_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox13_TextChanged(object sender, EventArgs e)
        {

        }

        private void save_option_Click_1(object sender, EventArgs e)
        {
            try
            {
                da.Update((DataTable)bs.DataSource);
            }
            catch
            {
                MessageBox.Show("لطفا در ورود شناسه دقت فرمایید");
            }
            id_txtbox.Text = "";
        }


        private void save_btn_Click_1(object sender, EventArgs e)
        {
            try
            {
                sqlquery.open();
                int empid = -1;
                empid = (int)sqlquery.ExecuteScalar("SELECT employeid FROM profile where profile.name =N'" + textBox10.Text.Trim() + "' and profile.family=N'" + textBox13.Text.Trim() + "'");
                string q = "INSERT INTO mission (employeid,day,month,year,beginhour,beginmin,endhour,endmin,code)VALUES(" + empid.ToString().Trim() + "," + persianDateTimePicker2.Value.Day.ToString() + "," + persianDateTimePicker2.Value.Month.ToString() + "," + persianDateTimePicker2.Value.Year.ToString() + "," + beginhournumeric.Value + "," + beginminnumeric.Value + "," + endhournumeric.Value + "," + endminnumeric.Value + "," + textBox1.Text.Trim() + ")";
                sqlquery.ExecuteNonQuery(q);
                sqlquery.close();
            }
            catch
            {
                MessageBox.Show("لطفا اطلاعات را درست وارد کنید");
            }
            textBox10.Text = "";
            textBox13.Text = "";
            textBox1.Text = "";
        }

        private void show_option_Click_2(object sender, EventArgs e)
        {
            string q;
            try
            {
                sqlquery.open();
                if (textBox2.Text.Length > 0 || textBox11.Text.Length > 0)
                    q = "SELECT id,mission.employeid as[شماره کارمندی],rtrim(name)+space(1)+ltrim(family) as [نام و نام خانوادگی],ltrim(str(year))+'/'+ltrim(str(month))+'/'+ltrim(str(day)) as [تاریخ],ltrim(str(beginhour))+':'+ltrim(str(beginmin)) as [ساعت شروع],ltrim(str(endhour))+':'+ltrim(str(endmin)) as [ساعت پایان],code as[نوع ماموریت] FROM mission,profile WHERE profile.employeid=mission.employeid and mission.employeid IN (SELECT profile.employeid FROM profile WHERE name=N'" + textBox2.Text.Trim() + "' or family=N'" + textBox11.Text.Trim() + "' )";
                else
                    q = "SELECT TOP 150 id,mission.employeid as[شماره کارمندی],rtrim(name)+space(1)+ltrim(family) as [نام و نام خانوادگی],ltrim(str(year))+'/'+ltrim(str(month))+'/'+ltrim(str(day)) as [تاریخ],ltrim(str(beginhour))+':'+ltrim(str(beginmin)) as [ساعت شروع],ltrim(str(endhour))+':'+ltrim(str(endmin)) as [ساعت پایان],code as[نوع ماموریت] FROM mission,profile WHERE profile.employeid=mission.employeid";
                da = sqlquery.sqlcommand(q);
                table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                da.Fill(table);
                bs.DataSource = table;
                dataGridView1.DataSource = bs;
                sqlquery.close();
            }
            catch
            {
                MessageBox.Show("مشکلی رخ داده است");
            }
            //setheader();
            id_txtbox.Text = "";
        }

        private void textBox11_TextChanged(object sender, EventArgs e)
        {

        }

        private void id_txtbox_TextChanged_1(object sender, EventArgs e)
        {
            sqlquery.open();
            da = sqlquery.sqlcommand("SELECT * FROM mission WHERE employeid like '%" + id_txtbox.Text.Trim() + "%'");
            table = new DataTable();
            da.Fill(table);
            bs.DataSource = table;
            dataGridView1.DataSource = bs;
            sqlquery.close();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int currentRow = int.Parse(e.RowIndex.ToString());

            if (dataGridView1.Columns[e.ColumnIndex] == deleteButton && currentRow >= 0)
            {
                DialogResult msg = MessageBox.Show("Are You Sure?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (msg == DialogResult.OK)
                {
                    sqlquery.open();
                    sqlquery.ExecuteNonQuery("DELETE mission WHERE id=" + dataGridView1[2, currentRow].Value);
                    sqlquery.close();
                    dataGridView1.Rows.RemoveAt(currentRow);
                }
            }
            else if (dataGridView1.Columns[e.ColumnIndex] == editButton && currentRow >= 0)
            {
                editemission edform = new editemission(sqlquery, dataGridView1[2, currentRow].Value.ToString(), dataGridView1[3, currentRow].Value.ToString(), dataGridView1[4, currentRow].Value.ToString(), dataGridView1[5, currentRow].Value.ToString(), dataGridView1[6, currentRow].Value.ToString(), dataGridView1[7, currentRow].Value.ToString(), dataGridView1[8, currentRow].Value.ToString());
                edform.Show();
            }
        }

        private void dataGridView1_DataBindingComplete_1(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (!load)
            {
                setheader();

                editButton = new DataGridViewButtonColumn();
                editButton.HeaderText = "ویرایش";
                editButton.Text = "ویرایش";
                editButton.UseColumnTextForButtonValue = true;
                editButton.Width = 80;
                dataGridView1.Columns.Add(editButton);

                deleteButton = new DataGridViewButtonColumn();
                deleteButton.HeaderText = "حذف";
                deleteButton.Text = "حذف";
                deleteButton.UseColumnTextForButtonValue = true;
                deleteButton.Width = 80;
                dataGridView1.Columns.Add(deleteButton);
                load = true;
            }
        }
    }
}
